<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../../css//layui.css"/>
    <style>
        body {
            background-color: #efefef;
        }

        tr, th {
            text-align: center !important;
        }

        table {
            text-align: center;
        }

        .active {
            color: #ff3c3c;
            font-weight: 600;
        }
    </style>
</head>

<body>
<div id="app">
    <div class="layui-panel">
        <div style="padding: 32px;">

            <div class="layui-form">
                <input type="text" name="" placeholder="请输入科目" class="layui-input" v-model.trim="subjectName">
                <hr class="ws-space-16">
                <input type="text" name="" placeholder="请输入分数" class="layui-input" v-model.number="subjectScore">
            </div>

            <button type="button" class="layui-btn layui-btn-normal" @click="addSubject">添加</button>

            <table class="layui-table">
                <thead>
                <tr>
                    <th>编号</th>
                    <th>科目</th>
                    <th>成绩</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="(item,index) in list" :key="item.id">
                    <td>{{index + 1}}</td>
                    <td>{{item.subject}}</td>
                    <td :class="{active:item.score<60}">{{item.score}}</td>
                    <td>
                        <button type="button" class="layui-btn layui-btn-danger" @click="deleteSubject(item.id)">删除</button>
                    </td>
                </tr>
                </tbody>
            </table>


        </div>
    </div>
    <div class="layui-panel">
        <div style="padding: 32px;">
            <p>总分：{{totalScore}}</p>
            <p>平均分：{{averageScore.toFixed(2)}}</p>
        </div>
    </div>

</div>
<script src="../../javascript//vue.js"></script>
<script>

    const vm = new Vue({
        el: "#app",
        data() {
            return {
                list: JSON.parse(localStorage.getItem("list")) || [],
                subjectName: "",
                subjectScore: "",
            }
        },
        methods: {
            //删除
            deleteSubject(id) {
                this.list = this.list.filter(item => item.id !== id)
            },
            //添加
            addSubject() {
                if (this.verifySubjectName(this.subjectName) && this.verifySubjectScore(this.subjectScore)) {
                    const object = {ia: Date.now(), subject: this.subjectName, score: this.subjectScore}
                    this.list.unshift(object)
                }
            },
            //验证科目名字
            verifySubjectName(name) {
                if (name === "") {
                    return alert("您输入的科目有问题")
                }

                return true
            },
            //验证分数
            verifySubjectScore(score) {
                if (score === "") {
                    return alert("您输入的分数有问题")
                }
                return true
            }
        },
        computed: {
            // 总分
            totalScore() {
                return this.list.reduce((sum, item) => {
                    return sum += item.score
                }, 0)
            },
            // 平均分
            averageScore() {
                return (this.totalScore / this.list.length) || 0
            }
        },
        watch: {
            // 持久化存储
            list: {
                deep: true,
                handler(newList) {
                    localStorage.setItem("list", JSON.stringify(newList))
                }
            }
        }
    })
</script>
</body>

</html>